//app.js

App({
  onShow:function(e){
  },
  onLaunch: function (e) {
    var that = this;
    var scene = e.scene;
    wx.setStorageSync('scene', scene)
    console.log(e)

    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    
    
    var requestpath = that.siteinfo.rootpath;

    // 登录
    wx.login({
      success: res => {
        if (res.code) {
          var code = res.code
          wx.request({
            url: requestpath + 'addUser',
            data: { code: code, scene: scene },
            method: 'get',
            success: function (res) {
              if (res.data.code == 200) {
                wx.setStorageSync('userinfo', res.data.userinfo)
                wx.setStorageSync('user_id', res.data.userinfo.id)
                wx.setStorageSync('sessionkey', res.data.sessionkey)
              } else {
                console.error('登陆失败');
              }
              wx.hideLoading()
            }
          })
        }
      }
    })
  }, 
  onGotUserInfo: function (e) {
    var that = this;
    var scene = wx.getStorageSync('scene');
    wx.login({
      success: res => {
        if (res.code) {
          var code = res.code
          wx.showLoading({
            title: '',
          })
          wx.getUserInfo({
            lang: 'zh_CN',
            success: function (res) {

              var rawData = res.rawData
              wx.request({
                url: that.siteinfo.rootpath + 'login',
                data: { rawData: rawData, code: code, scene: scene },
                method: 'get',
                success: function (res) {
                  wx.hideLoading()
                  if (res.data.code == 200) {
                    that.siteinfo.islogin = true
                    that.siteinfo.userinfo = res.data.userinfo
                    wx.setStorageSync('userinfo', res.data.userinfo)
                    wx.setStorageSync('sessionkey', res.data.userinfo.sessionkey)
                  } else {
                    console.error('登陆失败');
                  }
                }
              })
            },
            fail: function () {
              // wx.redirectTo({
              //   url: '/pages/welcome/welcome?scene=' + scene
              // })
            }
          })
        }
      }
    })

  },
  globalData: {
    userInfo: null
  },
  siteinfo:require('siteinfo.js')
})